package com.gu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gu.entity.EduVideo;
import com.gu.entity.vo.VideoVo;
import com.boot.exception.DiyException;
import com.gu.service.EduVideoService;
import com.gu.mapper.EduVideoMapper;
import com.gu.service.VodClients;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo>
    implements EduVideoService{

    @Resource
    private VodClients vodClients ;

    //根据VideoId删除 小节
    @Override
    @Transactional
    public boolean deleteByVideoId(String videoId) {
        //1.防止前端恶意 进入页面攻击 先查后删除
        boolean result = false ;
        EduVideo video = this.getById(videoId);
        String videoSourceId = video.getVideoSourceId();
        if(!StringUtils.isEmpty(video.getTitle()) && !StringUtils.isEmpty(videoSourceId)){
            //2.微服务调用vod删除 云端视频 再删除课程内容
            vodClients.deleteVideoByVideoId(videoSourceId);
            result = this.removeById(videoId);
        }
        return result ;
    }

    //新增小节
    @Override
    @Transactional
    public boolean save(VideoVo videoVo) {
        boolean result = false ;
        //后端校验
        if(StringUtils.isEmpty(videoVo.getTitle()) || StringUtils.isEmpty(videoVo.getChapterId()) || StringUtils.isEmpty(videoVo.getCourseId())){
            throw new DiyException("400","章节编号和课程编号，章节标题不能置空");
        }else {
            EduVideo eduVideo = new EduVideo();
            BeanUtils.copyProperties(videoVo,eduVideo);
            result = this.save(eduVideo);
        }
        return result ;
    }

    //更新小节
    @Override
    public boolean updateById(VideoVo videoVo) {
        boolean result =false ;
        if(StringUtils.isEmpty(videoVo.getTitle()) || StringUtils.isEmpty(videoVo.getChapterId()) || StringUtils.isEmpty(videoVo.getId())
                || StringUtils.isEmpty(videoVo.getCourseId())){
          throw new DiyException("400","章节编号和课程编号，章节标题,编号 不能置空");
        }else {
            EduVideo eduVideo = new EduVideo();
            BeanUtils.copyProperties(videoVo,eduVideo);
            result = this.updateById(eduVideo);
        }
        return result ;
    }

    //deleteByCourseId，
    @Override
    @Transactional
    public boolean removeByCourseId(String courseId) {
        //1.先查后判断：
        boolean result = false ;
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id",courseId);
        List<EduVideo> videoList = this.list(videoWrapper);
        // 1.2 再验证！
        if(videoList.size()>0){
            videoList.forEach(video -> {
                String videoSourceId = video.getVideoSourceId();
                //2.如果小节存在 视频 文件 就删除 ，没有则不处理
                if (!StringUtils.isEmpty(videoSourceId)){
                    vodClients.deleteVideoByVideoId(videoSourceId);
                }
            });
            result = this.remove(videoWrapper);
        }else {
            result = false ;
        }
        return result;
    }

}




